//
//  XQBaseController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/1/27.
//

import UIKit
import CoreLocation

class XQBaseController: UIViewController,CLLocationManagerDelegate {
    
    public var startTime : Int64 = 0
    public var endTime : Int64 = 0
    
    public var longitude: CLLocationDegrees = 0.0
    public var latitude: CLLocationDegrees = 0.0
    
    private lazy var locationManager : CLLocationManager = {
       let locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        return locationManager
    }()
    
    public lazy var orderId : String = ""
    
    public lazy var navTitle : String = "" {
        didSet {
            self.navigationBar.title = navTitle
        }
    }
    
    public lazy var navTitleColor : UIColor = .xq_black {
        didSet {
            self.navigationBar.titleColor = navTitleColor
        }
    }
    
    public lazy var backIcon : String = "" {
        didSet {
            self.navigationBar.backIcon = backIcon
        }
    }    
    
    public lazy var navigationBar : XQNavBarView = {
        let nav = XQNavBarView(frame: CGRectMake(0, 0, UIDevice.xq_width(), UIDevice.xq_topHeight()))
        nav.backHandle = {[weak self] in
            guard let sronfigy = self else {return}
            sronfigy.onBack()
        }
        return nav
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.modalPresentationStyle = .fullScreen
        makeUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        startL()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        stopL()
    }
    
    private func makeUI() {
        self.view.addSubview(navigationBar)
    }
    
    public func onBack() {
        self.view.endEditing(true)
        
        if let navigationController = self.navigationController {
            if navigationController.viewControllers.count > 1 {
            navigationController.popViewController(animated: true)
            } else if let presentingVC = navigationController.presentingViewController {
                presentingVC.dismiss(animated: true, completion: nil)
            }
        } else if self.presentingViewController != nil {
            dismiss(animated: true, completion: nil)
        }
    }
    
    public func startL() {
        stopL()
        self.locationManager.startUpdatingLocation()
    }
    
    public func stopL() {
        self.locationManager.stopUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        manager.stopUpdatingLocation()
        guard let currentLocation = locations.last else { return }
                
        let coordinate = currentLocation.coordinate
        longitude = coordinate.longitude
        latitude = coordinate.latitude
    }


}
